package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * AxdrInteger8类
 * 实现8位整数数据的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class AxdrInteger8 implements AxdrType {
    private byte value = 0;
    private byte[] dataCode = null;

    public AxdrInteger8() {
    }

    public AxdrInteger8(byte value) {
        this.value = value;
    }

    public byte getValue() {
        return value;
    }

    public void setValue(byte value) {
        this.value = value;
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        // 解码1个字节的整数
        this.value = (byte) input.read();
        return 1;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        // 编码1个字节的整数
        try {
            output.write(this.value);
        } catch (java.io.IOException e) {
            throw new RuntimeException(e);
        }
        return 1;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        return String.valueOf(this.value);
    }
}